package com.ljj.first.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljj.first.common.QueryPageParam;
import com.ljj.first.common.Result;
import com.ljj.first.entity.User;
import com.ljj.first.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class HelloComtroller {
    @GetMapping
    public String hello() {return "Hello World";}

    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    //增
    @PostMapping("/save")
    public boolean save(@RequestBody User user) {
        return userService.save(user);
    }
    //改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user) {
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }
    //delete
    @PostMapping("/delete")
    public boolean delete(Integer id) {
        return userService.removeById(id);
    }
//分页
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);

        System.out.println("num===" + query.getPageNum());
        System.out.println("size===" + query.getPageSize());

        HashMap param = query.getParam();
        String name = (String) param.get("name");
        System.out.println("param===" + (String)param.get("name"));
        //分页
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getName, name);

        IPage result = userService.page(page, lambdaQueryWrapper);

        System.out.println("total===" + result.getCurrent());

        return Result.suc(result.getRecords());
    }


}
